<!--  -->
<template>
  <div class="">
    <div v-if="token ? false : true" class="noLo">
      <div class="container">
        <div class="noLogin_img">
          <img src="@/assets/img/onstudy-nologin.png" alt="" />
        </div>
        <div class="noLogin_btn" @click="goto('/login')">登录查看</div>
      </div>
    </div>
    <div class="all" v-show="token ? true : false">
      <div class="tab_top">
        <div class="container">
          <div class="tab_item" :class="{ active: tabIndex == 0 }" @click="changeTabIndex(0)">
            订阅课程
          </div>
          <div class="tab_item" :class="{ active: tabIndex == 1 }" @click="changeTabIndex(1)">
            收藏课程
          </div>
        </div>
      </div>

      <div class="list" v-show="token ? true : false">
        <div class="container">
          <div class="lesson_list" ref="listHeight">
            <div
              class="lesson_item"
              v-for="item in infoList.data"
              :key="item.id"
              @click="gotoDetail(item.id)"
            >
              <div class="img_box">
                <img :src="item.thumb" alt="" />
              </div>

              <div class="info">
                <p class="lesson_tit" v-text="item.title"></p>
                <div>
                  <span class="star_num" v-text="item.user_count + '人已订阅'"></span>
                  <span v-if="item.is_free == 1" class="price_free" v-text="'免费'"></span>
                  <span v-else class="price" v-text="'￥' + item.charge"></span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <tab-bar :prop="'onStudy'"></tab-bar>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

import tabBar from "@/components/tabBar";
export default {
  //import引入的组件需要注入到对象中才能使用
  name: "onStudy",
  components: {
    tabBar,
  },
  data() {
    //这里存放数据
    return {
      starList: [],
      buyList: [],
      infoList: [],
      page: 1,
      page_size: 10,
      lazyFlag: true,
      tabIndex: 0,
      token: localStorage.getItem("h5-token"),
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    changeTabIndex(num) {
      this.tabIndex = num;
      if (num == 0) {
        this.infoList = this.starList;
      }
      switch (num) {
        case 0:
          this.infoList = this.starList;
          break;
        case 1:
          this.infoList = this.buyList;
          break;
      }
    },
    goto(path) {
      this.$router.push({
        path,
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  async created() {
    if (this.token) {
      await this.$axios
        .get("http://1.14.239.98/api/v2/member/courses", {
          // params:{
          //   page:this.page,
          //   page_size:this.page_size,
          // }
        })
        .then((res) => {
          // console.log('分类数据',res);
          // console.log(res);
          if (res.code == 0) {
            this.starList = res.data;
            this.infoList = this.starList;
            // console.log("this.start", this.starList);
          }
        });
      await this.$axios
        .get("http://1.14.239.98/api/v2/member/courses/like", {
          // params:{
          //   page:this.page,
          //   page_size:this.page_size,
          // }
        })
        .then((res) => {
          // console.log('分类数据',res);
          // console.log(res);
          if (res.code == 0) {
            this.buyList = res.data;
            // console.log("this.buy", this.buyList);
          }
        });
    }
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped lang="less">
div.noLo {
  width: 100%;
  div.container {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    div.noLogin_img {
      height: 4.48rem;
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 2.4rem;
        height: 2.4rem;
      }
    }

    div.noLogin_btn {
      width: 6.9rem;
      height: 0.92rem;
      line-height: 0.92rem;
      background: #3ca7fa;
      border-radius: 0.08rem;
      text-align: center;
      color: #fff;
      font-size: 0.32rem;
      font-weight: 600;
    }
  }
}

div.tab_top {
  height: 1rem;
  width: 100%;
  border-bottom: 10px solid #f3f6f9;
  box-sizing: content-box;
  .container {
    height: 1rem;
    width: 6.9rem;
    display: flex;
    justify-content: space-around;
    align-items: center;
    div.tab_item {
      height: 1rem;
      font-size: 0.32rem;
      color: #666;
      line-height: 1rem;
      // margin-right: 0.32rem;
    }
    div.tab_item.active {
      color: #3ca7fa;
      font-weight: bold;
    }
  }
}

div.list {
  background-color: #fff;
  padding-top: 0.3rem;
  div.container {
    width: 6.9rem;
    display: flex;
    flex-direction: column;
    div.tit {
      width: 345px;
      height: 0.38rem;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 0.5rem;
      span:nth-child(1) {
        font-size: 0.36rem;
        color: #333;
      }
      span:nth-child(2) {
        font-size: 0.24rem;
        color: #999;
      }
    }

    div.lesson_list {
      width: 6.9rem;
      display: flex;
      flex-direction: column;
      div.lesson_item {
        width: 6.9rem;
        height: 1.8rem;
        display: flex;
        justify-content: space-between;
        margin-bottom: 0.3rem;
        div.img_box {
          width: 2.4rem;
          height: 1.8rem;

          img {
            width: 2.4rem;
            height: 1.8rem;
            border-radius: 0.08rem;
          }
        }

        div.info {
          display: flex;
          width: 4.3rem;
          height: 1.8rem;
          flex-direction: column;
          justify-content: space-between;
          p.lesson_tit {
            margin-top: 0.1rem;
            font-size: 0.3rem;
            // margin-bottom: .3rem;
            text-align: start;
          }

          div {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 0.1rem;
            span.star_num {
              color: #999;
              font-size: 0.24rem;
            }

            span.price {
              color: #ff5858;
              font-size: 0.36rem;
              font-weight: bolder;
            }
            span.price::first-letter {
              font-size: 0.28rem;
            }
            span.price_free {
              color: #52c41a;
              font-size: 0.28rem;
              font-weight: bolder;
            }
          }
        }
      }
    }
  }
}
</style>
